<?php

/**
*
*/
class select_charact extends Load_use
{

    function getBlock()
    {
        $page_id	= $this->getParam( 'page_id', false );
        $parents	= $this->getParam( 'parents', false );
		
        if( !$page_id || !$parents )
            return false;

		if( isset( $_POST['filter_on'] ) )
		{
            $this->conn->query('DELETE FROM `store_product_filter` WHERE `page_id` = '.$page_id );
			
			if( isset( $_POST['filter'] ) )
			{
				$values = array();
				foreach( $_POST['filter'] as $field_id )
					$values[] = '( '.$page_id.', '.$field_id.' )';				
				
				if( count( $values ) )
					$this->conn->query( 'INSERT INTO store_product_filter ( `page_id`, `field_id` ) VALUES '.implode( ', ', $values ) );
			}
			
			$values = array();
			$this->conn->query('DELETE FROM `store_p_charact_fields_sort` WHERE `page_id` = '.$page_id );
 			foreach( $_POST['filter_zin'] as $field_id => $zin ) {
				if( $zin )
					$values[] = '( '.$page_id.', '.$field_id.', '.$zin.' )';
			}

			if( count( $values ) )
				$this->conn->query( 'INSERT INTO store_p_charact_fields_sort ( `page_id`, `field_id`, `zin` ) VALUES '.implode( ', ', $values ) );
        }
		
		$page_id = $_GET['edit_id'];
		
		
		// return false;
		 
		 
        $this->conn->query('
						SELECT d.field_id, fl.name, ft.page_id, fs.zin FROM store_p_charact_datas as d
						INNER JOIN store_products_cache AS p ON p.product_code = d.product_code
						INNER JOIN store_p_charact_fields AS fl ON fl.id = d.field_id
						LEFT JOIN store_product_filter AS ft ON ft.field_id = d.field_id AND ft.page_id = '.$page_id.'
						LEFT JOIN store_p_charact_fields_sort AS fs ON fs.field_id = d.field_id AND fs.page_id = '.$page_id.'
						WHERE p.parents = "'.$parents.'"
						GROUP BY d.field_id
						ORDER BY fl.name
		');

        if( !$this->conn->rowCount() )
            return false;

		$fields = $fields_null = array();
		while( $row = $this->conn->fetch() )
		{
			if( $row->zin ) 
				$fields[$row->zin] = $row;
			else
				$fields_null[] = $row;
		}
		ksort( $fields );
		
        $html_list = '';
		foreach( $fields as $row )
            $html_list .= '
					<tr>
						<td><input type="text" name="filter_zin['.$row->field_id.']" value="'.$row->zin.'" class="filter_zin_text"/></td>
						<td><label for="filter_'.$row->field_id.'">'.$row->name.'</label></td>
						<td><input type="checkbox" name="filter['.$row->field_id.']"  id="filter_'.$row->field_id.'" value="'.$row->field_id.'"'.( $row->page_id ? ' checked="checked" ' : '' ).'/></td>
					</tr>';
					
		foreach( $fields_null as $row )
            $html_list .= '
					<tr>
						<td><input type="text" name="filter_zin['.$row->field_id.']" value="'.$row->zin.'" class="filter_zin_text"/></td>
						<td><label for="filter_'.$row->field_id.'">'.$row->name.'</label></td>
						<td><input type="checkbox" name="filter['.$row->field_id.']"  id="filter_'.$row->field_id.'" value="'.$row->field_id.'"'.( $row->page_id ? ' checked="checked" ' : '' ).'/></td>
					</tr>';		
		
        $this->view->gText.='
				<style>
					.filter_fields{
						margin: 10px;
					}
					.filter_zin_text{
						width: 30px!important;
					}
					.filter_fields table td{
						padding: 0 10px;
					}
					.filter_fields table tr:first-child td{
						font-size: 16px;
						font-weight : bold;
						padding-bottom: 10px;
					}
				</style>';

        return '<div class="filter_fields">
					<input type="hidden" name="filter_on" />
					<table>
						<tr><td>Сорт.</td><td>Название характеристики</td><td>Фильтр</td></tr>
						'.$html_list.'
					</table>
				</div>';
    }
}
